package zdims.control
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import mx.collections.ArrayCollection;
	import mx.controls.Image;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.MoveEvent;
	import mx.managers.PopUpManager;
	import zdims.drawing.CoordinateType;
	import zdims.event.IMSMapEvent;
	import zdims.event.IMSMapMoveEvent;
	import zdims.event.IMSMapZoomEvent;
	import zdims.map.IMSMap;
	import zdims.mark.IMSMark;
	import zdims.mark.MarkBase;
	import zdims.mark.MarkLayer;
	import zdims.util.IMSOperType;
	import zdims.util.RectBound;

	public class MultiMarker extends Image
	{
		private var imsMap:IMSMap=FlexGlobals.topLevelApplication.imsMap;
		private var markerLayer:MarkLayer=FlexGlobals.topLevelApplication.markLayer;
		private var imsMark:IMSMark;
		private var imsContentMark:IMSMark;
		private var imgSrc:String;
		private var pntCoords:ArrayCollection;
		private var markerInfo:MarkerContent;
		private var haspopup:Boolean=false; //MarkerContent是否显示
		public var tagMark:IMSMark=null;
		public var logicX:Number=0;
		public var logicY:Number=0;

		public function get ImsMap():IMSMap
		{
			return imsMap;
		}

		public function set ImsMap(value:IMSMap):void
		{
			imsMap=value;
		}

		public function MultiMarker(pntCoords:ArrayCollection, imgSrc:String)
		{
			this.pntCoords=pntCoords;
			this.imgSrc=imgSrc;
		}

		//在地图上显示当前可视范围内的标注
		public function showPnts():void
		{
			var winLog:RectBound=this.imsMap.winViewBound;
			for (var i:int=0, j:int=pntCoords.length; i < j; i++)
			{
				var p:Object=pntCoords[i];
				var img:Image=this.markerLayer.getChildByName(p.Id) as Image;
				if (p.X <= winLog.xMax && p.X >= winLog.xMin && p.Y <= winLog.yMax && p.Y >= winLog.yMin)
				{
					if (img == null)
					{
						img=new Image();
						img.source=this.imgSrc;
						img.id=p.Id;
						img.name=p.Id;
						img.buttonMode=true;
						img.useHandCursor=true;
						img.visible=true;
						img.includeInLayout=true;
						img.data=p;
						imsMark=new IMSMark(img, CoordinateType.Logic);
						imsMark.x=p.X;
						imsMark.y=p.Y;
						imsMark.enableAnimation=false;
						this.markerLayer.addMark(imsMark);
						imsMark.mouseClickCallback=onMarkClick;
					}
				}
				else if (img != null)
				{
					img.visible=false;
					img.includeInLayout=false;
				}
			}
		}

		private function onMarkClick(mark:IMSMark, e:MouseEvent):void
		{
			this.imsMap.setCurOper(IMSOperType.None);
			var img:Image=e.currentTarget as Image;
			if (this.markerInfo == null)
			{
				this.markerInfo=new MarkerContent();
				this.markerInfo.id="markerInfo";
				this.markerInfo.name="markerInfo";
				this.markerInfo.addEventListener(CloseEvent.CLOSE, onMarkerInfoClose);

				this.markerInfo.visible=true;
				this.markerInfo.setMarkerContent(img.data.Ksbh, img.data.Ksmc, img.data.Cl, img.data.Chl, img.data.Kchsl, img.data.Xkhsl, img.data.Zhlyl);
				markerInfo.x=img.x + 15;
				markerInfo.y=img.y + 15;

				this.markerLayer.addElement(markerInfo);
			}
			else
			{
				this.markerInfo.visible=true;
				this.markerInfo.setMarkerContent(img.data.Ksbh, img.data.Ksmc, img.data.Cl, img.data.Chl, img.data.Kchsl, img.data.Xkhsl, img.data.Zhlyl);
				markerInfo.x=img.x + 15;
				markerInfo.y=img.y + 15;
			}

			if (tagMark != null) //删除原来关联mark的修正回调方法
				tagMark.markReviseOverCallback=null;

			tagMark=mark; //新的关联mark

			if (tagMark != null) //添加新的关联mark的修正回调方法
				tagMark.markReviseOverCallback=markReviseOverCallback;
		}

		private function markReviseOverCallback(mark:MarkBase):void
		{ //修正标注位置
			if (markerInfo != null)
			{
				//mark.target,关联标注信息
				//修正位置
				markerInfo.x=mark.markControl.x + 15;
				markerInfo.y=mark.markControl.y + 15;
			}
		}

		public function removeAllPnt():void
		{
			this.markerLayer.removeAllChildren();
		}

		/**
		 * 通过标注的ID号获得当前地图上的标注
		 * */
		public function showPntById(id:String):void
		{
			var img:Image=this.markerLayer.getChildByName(id) as Image;
			if (img == null)
			{
				for (var i:int=0, j:int=pntCoords.length; i < j; i++)
				{
					var p:Object=pntCoords[i];
					if (img == null)
					{
						img=new Image();
						img.source=this.imgSrc;
						img.id=p.Id;
						img.name=p.Id;
						img.buttonMode=true;
						img.useHandCursor=true;
						img.visible=true;
						img.includeInLayout=true;
						img.data=p;
						imsMark=new IMSMark(img, CoordinateType.Logic);
						imsMark.x=p.X;
						imsMark.y=p.Y;
						imsMark.enableAnimation=false;
						this.markerLayer.addMark(imsMark);
						imsMark.mouseClickCallback=onMarkClick;
						this.imsMap.setCenter(img.data.X, img.data.Y, this.imsMap.levelNum - 1);
						img.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
					}
				}
			}
			else
			{

				this.imsMap.setCenter(img.data.X, img.data.Y, this.imsMap.levelNum - 1);
				img.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));

			}
		}

		/**
		 * 关闭标注信息窗口
		 */
		private function onMarkerInfoClose(e:Event):void
		{
			this.markerInfo.visible=false;
			this.markerInfo=null;
			this.haspopup=false;
		}
	}
}